﻿using System;
using System.Diagnostics;
using System.Net.Security;

//平台需要提供的接口
//ttform 比ttengine 层级要更高一些，可以内嵌到其它引擎中
//这要求我们不使用Shader 这一类资源
public enum LogLevel
{
    Normal,
    Warning,
    Error,
}
public interface IEnv_Impl
{
    //平台特性
    IPlatform Platform { get; }

    //绘图接口
    IGraphic Graphic { get; }

    //输入接口
    IInput Input { get; }

    ////IO接口
    //IFileIO FileIO { get; }

    //声音接口
    IAudio Audio { get; }

    IPackGroup RootPack { get; }
    void Log(string message, LogLevel level = LogLevel.Normal);
}



public static class Env
{
    static IEnv_Impl iEnv;

    public static void _Init(IEnv_Impl env)
    {
        if (iEnv != null)
            throw new Exception("already inited");
        iEnv = env;

        Platform = env.Platform;
        Graphic = env.Graphic;
        Input = env.Input;
        //FileIO = env.FileIO;
        Audio = env.Audio;
        RootPack = env.RootPack;
    }
    public static void Log(string message, LogLevel level = LogLevel.Normal)
    {
        iEnv.Log(message, level);
    }

    public static IPlatform Platform { get; private set; }

    //绘图接口
    public static IGraphic Graphic { get; private set; }

    //输入接口
    public static IInput Input { get; private set; }

    public static IPackGroup RootPack { get; private set; }
    //IO接口
    //public static IFileIO FileIO { get; private set; }

    //声音接口
    public static IAudio Audio { get; private set; }


    /// <summary>
    /// 用一个明确的Set方法，避免出现 += 导致 multicastDelegate
    /// </summary>
    /// <param name="onupdate"></param>
    public static void SetEvent_OnUpdate(Action<float> onupdate)
    {
        _OnUpdate = onupdate;
    }
    public static void SetEvnet_OnChangeSize(Action<int, int> onChangeSize)
    {
        _OnChangeSize = onChangeSize;
    }
    //为啥不用event了，因为PureTS 并不支持 多播 delegate;
    //当只有一个回调时，他是delegate，再+= 第二个回调时，他就变成了multicastDelegate
    public static Action<int, int> _OnChangeSize;
    public static Action<float> _OnUpdate;
}
/// <summary>
/// 已知PuerTS问题
/// 1.对多播支持不行
///     解决方法，避免event 直接用，提供SetEvent_XXX 方法，避免多播delegate产生
///     
/// 2.接口没有走静态绑定
///     解决方法，包装class
///     
/// 3.数组支持没有
///     解决方法，包装JSArray
/// </summary>
public static class TT
{
    public static void Log(string message, LogLevel level = LogLevel.Normal)
    {
        Env.Log(message, level);
    }

    static TT()
    {
        RootPack = JSPackGroup._FromNative(Env.RootPack);
    }

    //    public static IPlatform Platform { get; private set; }

    //    //绘图接口
    //    public static IGraphic Graphic { get; private set; }

    //    //输入接口
    //    public static IInput Input { get; private set; }

    public static JSPackGroup RootPack { get; private set; }
    //    //IO接口
    //    //public static IFileIO FileIO { get; private set; }

    //    //声音接口
    //    public static IAudio Audio { get; private set; }


    public static void SetEvent_OnUpdate(Action<float> onupdate)
    {
        Env._OnUpdate = onupdate;
    }
    public static void SetEvnet_OnChangeSize(Action<int, int> onChangeSize)
    {
        Env._OnChangeSize = onChangeSize;
    }

}